home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Graphics Samples / ShapePart Browser ƒ / Main.c < prev    next >
Encoding:
Text File  |  1996-04-15  |  8.4 KB  |  505 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    Main.c
  3.  *
  4.  *    Robert Dierkes,  November 11, 1993
  5.  *
  6.  *    Change History:
  7.  *
  8.  *       11/93    ???        New
  9.  *       4/96        bob        Updated #includes to support changed GX Library names.
  10.  *                        Added the copyright info.
  11.  *
  12.  *
  13.  *        © Apple Computer, Inc. 1990 - 1996  All rights reserved
  14.  *
  15.  */
  16.  
  17.  
  18. #undef    MAC_HEADERS
  19.  
  20.  
  21. /*------------------*/
  22. /*    Include Files    */
  23. /*------------------*/
  24. #ifndef    MAC_HEADERS
  25.     #include <Memory.h>
  26.     #include <Fonts.h>
  27.     #include <Menus.h>
  28.     #include <OSEvents.h>
  29.     #include <Dialogs.h>
  30.     #include <ToolUtils.h>
  31.     #include <Desk.h>
  32.     #include <GestaltEqu.h>
  33. #endif    MAC_HEADERS
  34.  
  35. #include <GXEnvironment.h>
  36. #include "GraphicsLibraries.h"
  37. #include <GXErrors.h>
  38.  
  39. #include "ResourceIDs.h"
  40. #include "Window.h"
  41. #include "ShapeAction.h"
  42. #include "AutoMouse.h"
  43.  
  44. #include "Main.h"
  45.  
  46.  
  47. /*----------------------*/
  48. /*    Global Declarations    */
  49. /*----------------------*/
  50. gxGraphicsClient    gClient;
  51. Boolean                gQuitApp;
  52. Handle                ghMainMenu;
  53. Rect                gDragRect;
  54. WindowPtr            gWindow;
  55.  
  56.  
  57. /*------------------------------*/
  58. /*    External Declarations     */
  59. /*------------------------------*/
  60. extern    GlobalStructure    globals;
  61.  
  62.  
  63.     Boolean
  64. InitApp (void)
  65. {
  66.     long            response;
  67.     gxGraphicsError    grfxError;
  68.  
  69. #ifndef linkedIn
  70.     if (Gestalt (gestaltGraphicsVersion, &response)) {
  71.         DebugStr ("\pQuickDraw GX is not installed");
  72.         return false;
  73.     }
  74. #endif
  75.  
  76.     if ((gClient = GXNewGraphicsClient (nil, kGraphicsHeapSize, 0)) == 0) {
  77.         DebugStr ("\pGXNewGraphicsClient failed");
  78.         return false;
  79.     }
  80.  
  81.     /* Initialize Skia */
  82.     GXEnterGraphics ();
  83.  
  84.     GXSetValidation (gxNoValidation);
  85.     SetGraphicsLibraryErrors ();
  86.     SetGraphicsLibraryNotices();
  87.     InitCommonColors ();
  88.  
  89.     MaxApplZone();
  90.     MoreMasters();
  91.     MoreMasters();
  92.  
  93.     InitGraf (&qd.thePort);
  94.     InitFonts ();
  95.     FlushEvents (everyEvent, 0);
  96.     InitWindows ();
  97.     InitMenus ();
  98.     InitDialogs (nil);
  99.     InitCursor ();
  100.  
  101.     return true;
  102.  
  103. }    /* InitApp */
  104.  
  105.  
  106.     void
  107. ExitApp (void)
  108. {
  109.     gxViewPort    parent;
  110.  
  111.     if (gWindow)
  112.     {
  113.         if (parent = GXGetWindowViewPort (gWindow))
  114.             GXDisposeViewPort (parent);
  115.         DisposeDialog (gWindow);
  116.     }
  117.  
  118.        DisposeCommonColors ();
  119.     GXExitGraphics ();
  120.     if (gClient)
  121.         GXDisposeGraphicsClient (gClient);
  122.  
  123. }    /* ExitApp */
  124.  
  125.  
  126.     void
  127. InitializeMenus (void)
  128. {
  129.     ghMainMenu = GetNewMBar (menuBarRsrcID);
  130.     SetMenuBar (ghMainMenu);
  131.     AddResMenu (GetMHandle (appleMenuRsrcID), 'DRVR');
  132.     DrawMenuBar ();
  133.  
  134. }    /* InitializeMenus */
  135.  
  136.  
  137.     WindowPtr
  138. InitializeWindow (void)
  139. {
  140.     WindowPtr    pWindow;
  141.     Rect        bounds;
  142.     short        totalMargin;
  143.     Point        newPosition;
  144.     GDHandle    hGDevice;
  145.  
  146.     if ((pWindow = GetNewDialog (dialogRsrcID, nil, kWindowOnTop)) == nil)
  147.         return (nil);
  148.  
  149.     SetPort (pWindow);
  150.     SetDefaultViewPort (GXNewWindowViewPort (pWindow));
  151.  
  152.     /* Find the deepest screen */
  153.     SetRect (&bounds, -32767, -32767, 32767, 32767);
  154.     hGDevice = GetMaxDevice (&bounds);
  155.     bounds = (**hGDevice).gdRect;
  156.  
  157.     totalMargin = (bounds.right - bounds.left) - (pWindow->portRect.right - pWindow->portRect.left);
  158.     newPosition.h = bounds.left + (totalMargin >> 1);
  159.  
  160.     totalMargin = (bounds.bottom - bounds.top) - (pWindow->portRect.bottom - pWindow->portRect.top - kWindowTitleHeight);
  161.     newPosition.v = bounds.top + ((totalMargin + ((hGDevice == GetMainDevice ()) ? GetMBarHeight () : 0)) >> 1);
  162.  
  163.     MoveWindow (pWindow, newPosition.h, newPosition.v, true);
  164.     ShowWindow (pWindow);
  165.  
  166.     gDragRect = qd.screenBits.bounds;
  167.     return (pWindow);
  168.  
  169. }    /* InitializeWindow */
  170.  
  171.  
  172.     void
  173. DoContentClick (EventRecord *pEvent, WindowPtr pWindow)
  174. {
  175.     if (pWindow == nil)
  176.         return;
  177.  
  178.     if (FrontWindow () == pWindow)
  179.     {
  180.         if (pWindow == gWindow)
  181.             DoHitTestContent (pEvent, pWindow);
  182.     }
  183.     else
  184.     {
  185.         SetPort (pWindow);
  186.         SelectWindow (pWindow);
  187.     }
  188.  
  189. }    /*    DoContentClick  */
  190.  
  191.  
  192.     void
  193. DoMenuCommand (menuResult)
  194.     long        menuResult;
  195. {
  196.     short        menuID,
  197.                 itemNumber;
  198.  
  199.     if (! menuResult)
  200.         return;
  201.  
  202.     menuID       = HiWord (menuResult);
  203.     itemNumber = LoWord (menuResult);
  204.  
  205.     switch (menuID)
  206.     {
  207.         case appleMenuRsrcID:
  208.             switch (itemNumber)
  209.             {
  210.                 case itemAbout:
  211.                     Alert (aboutRsrcID, nil);
  212.                     break;
  213.  
  214.                 default:
  215. //                    DoDeskAcc (appleMenuRsrcID, itemNumber);
  216.                     SysBeep (1);
  217.                     break;
  218.             }
  219.             break;
  220.  
  221.         case fileMenuRsrcID:
  222.             switch (itemNumber)
  223.             {
  224.                 case itemQuit:
  225.                     gQuitApp = true;
  226.                     break;
  227.  
  228.                 default:
  229.                     SysBeep (1);
  230.                     break;
  231.             }
  232.             break;
  233.  
  234.         case metricsMenuRsrcID:
  235.             switch (itemNumber)
  236.             {
  237.                 case itemShowControlPoints:
  238.                 case itemShowLocalBounds:
  239.                     ToggleMetricsMenuItem (itemNumber, gWindow);
  240.                     break;
  241.  
  242.                 case itemAutoMouse:
  243.                     ToggleAutoMouse ();
  244.                     break;
  245.  
  246.                 default:
  247.                     SysBeep (1);
  248.                     break;
  249.             }
  250.             break;
  251.  
  252.         default:
  253.             break;
  254.     }  /*  switch (menuID)  */
  255.  
  256.     HiliteMenu (kHiliteAllMenus);
  257.  
  258. }    /*    DoMenuCommand  */
  259.  
  260.  
  261.     void
  262. DoGrowBox (EventRecord *pEvent, WindowPtr pWindow)
  263. {
  264.     long        newSize;
  265.     Rect        newRect,
  266.                 oldRect;
  267.  
  268.     oldRect = newRect = pWindow->portRect;
  269.  
  270.     if (newSize = GrowWindow (pWindow, pEvent->where, &qd.screenBits.bounds))
  271.     {
  272.         newRect.right    = newRect.left + (short) newSize;
  273.         newRect.bottom    = newRect.top  + (short) (newSize >> 16);
  274.         SizeWindow (pWindow,
  275.                     newRect.right - newRect.left,
  276.                     newRect.bottom - newRect.top,
  277.                     true);
  278.         InvalRect (&oldRect);
  279.     }
  280.  
  281. }    /*    DoGrowBox  */
  282.  
  283.  
  284.     void
  285. DoZoomBox (EventRecord *pEvent, WindowPtr pWindow, short windowPart)
  286. {
  287.     if (TrackBox (pWindow, pEvent->where, windowPart))
  288.     {
  289.         ZoomWindow (pWindow, windowPart, true);
  290.         InvalRect (&pWindow->portRect);
  291.     }
  292.  
  293. }    /*    DoZoomBox  */
  294.  
  295.  
  296.     void
  297. DoNullEvent (EventRecord *pEvent)
  298. {
  299.     IdleHitTestWindow (gWindow, pEvent);
  300.     MoveAutoMouse ();
  301.  
  302. }    /*    DoNullEvent  */
  303.  
  304.  
  305.     void
  306. DoMouseDown (EventRecord *pEvent)
  307. {
  308.     short            windowPart;
  309.     WindowPtr        whichWindow;
  310.  
  311.     windowPart = FindWindow (pEvent->where, &whichWindow);
  312.  
  313.     switch (windowPart)
  314.     {
  315.     case inDesk:
  316.         break;
  317.  
  318.     case inMenuBar:
  319.         DoMenuCommand (MenuSelect (pEvent->where));
  320.         break;
  321.  
  322.     case inSysWindow:
  323.         SystemClick (pEvent, whichWindow);
  324.         break;
  325.  
  326.     case inContent:
  327.         DoContentClick (pEvent, whichWindow);
  328.         break;
  329.  
  330.     case inDrag:
  331.         DragWindow (whichWindow, pEvent->where, &gDragRect);
  332.         break;
  333.  
  334.     case inGrow:
  335.         DoGrowBox (pEvent, whichWindow);
  336.         break;
  337.  
  338.     case inGoAway:
  339.           if (TrackGoAway (whichWindow, pEvent->where))
  340.           {
  341.               if (whichWindow == gWindow)
  342.                 gQuitApp = true;
  343.             else
  344.                 DebugStr ("\pDoMouseDown: Clicked in window of unknown close box");
  345.           }
  346.         break;
  347.  
  348.     case inZoomIn:
  349.     case inZoomOut:
  350.         DoZoomBox (pEvent, whichWindow, windowPart);
  351.         break;
  352.  
  353.     default:
  354.         break;
  355. }    /* switch */
  356.  
  357. }    /*    DoMouseDown  */
  358.  
  359.  
  360.     void
  361. DoKeyStroke (EventRecord *pEvent)
  362. {
  363.     char            charCode;
  364.  
  365.     charCode = pEvent->message & charCodeMask;
  366.  
  367.     if (pEvent->modifiers & btnState)
  368.     {
  369.         /*--------------*/
  370.         /* Button is UP */
  371.         /*--------------*/
  372.         if (pEvent->modifiers & cmdKey)
  373.         {
  374.             /*-------------------*/
  375.             /* Command key    x100 */
  376.             /*-------------------*/
  377.             DoMenuCommand (MenuKey (charCode));
  378.         }
  379.     }
  380.  
  381. }    /*    DoKeyStroke  */
  382.  
  383.  
  384.     void
  385. DoUpdate (EventRecord *pEvent)
  386. {
  387.     GrafPtr            savedPort;
  388.     WindowPtr        pUpdateWindow;
  389.  
  390.     GetPort (&savedPort);
  391.  
  392.     pUpdateWindow = (WindowPtr) pEvent->message;
  393.  
  394.     SetPort (pUpdateWindow);
  395.     BeginUpdate (pUpdateWindow);
  396.  
  397.     EraseRgn (pUpdateWindow->visRgn);
  398.  
  399.     if (pUpdateWindow == gWindow)
  400.         UpdateHitTestWindow (gWindow);
  401.  
  402.     EndUpdate (pUpdateWindow);
  403.     SetPort (savedPort);
  404.  
  405. }    /*    DoUpdate  */
  406.  
  407.  
  408.     void
  409. DoActivate (EventRecord *pEvent)
  410. {
  411.     WindowPtr        pActiveWindow;
  412.  
  413.     /*--------------------------------------*/
  414.     /* Get the window to de/activate        */
  415.     /* and its kind from the event message    */
  416.     /*--------------------------------------*/
  417.     pActiveWindow = (WindowPtr) pEvent->message;
  418.  
  419.     SetPort (pActiveWindow);
  420.  
  421.     /*-----------------*/
  422.     /* Activate window */
  423.     /*-----------------*/
  424.     if (pEvent->modifiers & activeFlag)
  425.     {
  426.         /* Enable/Disable items */
  427.     }
  428.     else
  429.     /*-------------------*/
  430.     /* Deactivate window */
  431.     /*-------------------*/
  432.     {
  433.         /* Enable/Disable items */
  434.     }
  435.  
  436. }    /*    DoActivate  */
  437.  
  438.  
  439.     void
  440. DoEvent  (void)
  441. {
  442.     EventRecord    theEvent;
  443.  
  444.     SystemTask ();        /* Handle desk accessories */
  445.  
  446.     GetNextEvent (everyEvent, &theEvent);
  447.  
  448.     switch (theEvent.what)
  449.     {
  450.     case nullEvent:
  451.         DoNullEvent (&theEvent);
  452.         break;
  453.  
  454.     case mouseDown:
  455.         DoMouseDown (&theEvent);
  456.         break;
  457.  
  458.     case keyDown:
  459.     case autoKey:
  460.         DoKeyStroke (&theEvent);
  461.         break;
  462.  
  463.     case updateEvt:
  464.         DoUpdate (&theEvent);
  465.         break;
  466.  
  467.     case activateEvt:
  468.         DoActivate (&theEvent);
  469.         break;
  470.  
  471.     case mouseUp:
  472.     case keyUp:
  473.     case diskEvt:
  474.     case networkEvt:
  475.     case driverEvt:
  476.     default:
  477.         break;
  478.     }  /* switch (theEvent.what)  */
  479.  
  480. }    /* DoEvent */
  481.  
  482.  
  483. main (void)
  484. {
  485.     gQuitApp = false;
  486.     if (InitApp ())
  487.     {
  488.         InitializeMenus ();
  489.         if (gWindow = InitializeWindow ())
  490.         {
  491.             if (InitializeHitTesting (gWindow))
  492.             {
  493.                 InitializeAutoMouse (globals.boxes);
  494.  
  495.                 while (! gQuitApp)
  496.                     DoEvent ();
  497.  
  498.                 CleanupHitTesting ();
  499.             }
  500.         }
  501.         ExitApp ();
  502.     }
  503.  
  504. }    /* main */
  505.